package controller;

import com.google.gson.Gson;
import dao.UsersDao;
import dao.impl.UsersDaoImpl;
import entity.Users;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/userServlet")
public class UserServlet extends HttpServlet {
    private UsersDao usersDao = new UsersDaoImpl() ;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       req.setCharacterEncoding("utf-8");
        String method =req.getParameter("method") ;
       if("query".equals(method)){
           this.doQuery(req,resp);
       }
       else if("del".equals(method)){
           this.doDel(req, resp);
       }else  if("add".equals(method)){
           this.doAdd(req, resp);
       }else  if("getUser".equals(method)){
           this.doGetUser(req, resp) ;
       }else if("up".equals(method)){
           this.doUp(req, resp);
       }

    }

    protected void doUp(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = Integer.parseInt(req.getParameter("id")) ;
        String username = req.getParameter("username") ;
        String address =req.getParameter("address") ;
        String email = req.getParameter("email") ;
        String gender = req.getParameter("gender") ;

        Users users = new Users() ;
        users.setId(id);
        users.setEmail(email);
        users.setName(username);
        users.setAddress(address);
        users.setGender(gender);
        int i= usersDao.update(users) ;

        this.doQuery(req,resp);




    }

    protected void doGetUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(req.getParameter("id")) ;
        Users users = usersDao.getUsers(id) ;
        PrintWriter out = resp.getWriter() ;
        Gson gson = new Gson() ;
        out.print(gson.toJson(usersDao.getUsers(id)));

        out.close();

    }

    protected void doAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username") ;
        String address =req.getParameter("address") ;
        String email = req.getParameter("email") ;
        String gender = req.getParameter("gender") ;

        Users users = new Users() ;
        users.setEmail(email);
        users.setName(username);
        users.setAddress(address);
        users.setGender(gender);
        int i= usersDao.add(users) ;

        this.doQuery(req,resp);




    }

    protected void doDel(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int id = Integer.parseInt(req.getParameter("id")) ;
        usersDao.del(id) ;
        this.doQuery(req, resp);

    }

    protected void doQuery(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Users> list = usersDao.query() ;
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter() ;
//        req.setAttribute("list",list);
//        req.getRequestDispatcher("userslist.jsp").forward(req,resp);

        Gson gson = new Gson() ;
        out.print(gson.toJson(usersDao.query()));

        out.close();
    }




}
